import {h, resolveComponent} from 'vue';

export const renderLabel = (option: any) => {
  if (option.href) {
    return h('a', {href: option.href, target: '_blank'}, option.name as string);
  }
  if (!option.children) {
    return h(
      resolveComponent('RouterLink'),
      {to: {path: option.path}},
      {default: () => option.name}
    );
  }
  return option.name;
};

export const renderIcon = (option: any) => {
  if (option.iconName) {
    return h(resolveComponent('FxIcon'), {name: option.iconName});
  }
  return false;
};

export const resolveOptions = <T>(first: boolean, options?: T[]): MenuOptions<T> => {
  const pathKey = 'path';
  const menuOptions: MenuOptions<T> = {
    menus: {},
    types: [],
    sides: {}
  };
  resolveChildren(menuOptions.menus, menuOptions.types, menuOptions.sides, pathKey, first, options);
  return menuOptions;
};

export const resolveChildren = <T>(menus: Record<string, T[]>, types: T[], sides: Record<string, string>, pathKey: string, first: boolean, options?: T[]): void => {
  if (options && options.length > 0) {
    for (let i = 0; i < options.length; i++) {
      const item = options[i];
      if (item && item.hasOwnProperty(pathKey)) {
        const newItem = getTypeMenus(item, pathKey, first);
        const children: T[] | undefined = newItem.children;
        const path = newItem[pathKey];
        delete newItem['children'];
        if (path) {
          const pathStr = path.toString();
          menus[pathStr] = children || [];
          resolveChildrenNextPath(sides, children, pathKey, pathStr);
          types.push(newItem);
        }
      }
    }
  }
};

export const resolveChildrenNextPath = (sides: Record<string, string>, menus: any[] | undefined, pathKey: string, topPath: string): void => {
  if (menus && menus.length > 0) {
    for (let i = 0; i < menus.length; i++) {
      const item = menus[i];
      if (item) {
        const children = item.children;
        if (children && children.length > 0) {
          resolveChildrenNextPath(sides, children, pathKey, topPath);
        } else {
          if (item.hasOwnProperty(pathKey)) {
            const path = item[pathKey];
            if (path) {
              const pathStr = path.toString();
              sides[pathStr] = topPath;
            }
          }
        }
      }
    }
  }
};

export const getTypeMenus = <T>(menu: T, pathKey: string, first: boolean): T => {
  const newMenu = Object.assign({}, menu);
  if (first) {
    const children: T[] = menu.children;
    if (!children || children.length === 0) {
      const nextMenu = Object.assign({}, newMenu);
      newMenu.children = [nextMenu];
      delete nextMenu['children'];
    }
  }
  // 重置路径为子的第一项的
  newMenu[pathKey] = getFirstMenuPath(newMenu, pathKey);
  return newMenu;
};

export const getTypesValueByPath = <T>(menuOptions: MenuOptions<T>, path?: string): string | undefined => {
  if (path) {
    const topPath = menuOptions.sides[path];
    if (topPath) {
      return topPath;
    }
  }
  return path;
};

export const getMenusByTypesValue = <T>(menuOptions: MenuOptions<T>, typesValue?: string): T[] | undefined => {
  if (typesValue) {
    return menuOptions.menus[typesValue];
  }
};

export const getFirstMenuPath = (menu: any, pathKey: string): any => {
  if (menu) {
    const children = menu.children;
    if (children && children.length > 0) {
      const firstMenu = children[0];
      if (firstMenu) {
        return getFirstMenuPath(firstMenu, pathKey);
      }
    }
  }
  return menu[pathKey];
};

export type MenuOptions<T> = {
  menus: Record<string, T[]>
  types: T[],
  sides: Record<string, string>,
}